//
//  QRCodeManager.m
//  gdemm
//
//  Created by 乔明超 on 16/6/17.
//  Copyright © 2016年 Goldencis. All rights reserved.
//

#import "QRCodeManager.h"
#import "UserDefaultsUtil.h"
#import "StringUtil.h"
#import "Algorithm.h"
#import "hcrc32.h"
@interface QRCodeManager()

@end
@implementation QRCodeManager
static QRCodeManager *mInstance;


/**
 *  获取QRCodeManager单例
 *
 *  @return 返回单列对象
 */
+ (instancetype)getInstance{
    if (mInstance == nil) {
        @synchronized([QRCodeManager class]) {
            if (mInstance == nil) {
                mInstance = [[QRCodeManager alloc]init];
            }
            
        }
    }
    return mInstance;
}

/**
 *  获取二维码信息
 *
 *  @return 二维码信息
 */
- (QRCodeInfo *)getInQrInfo{
    NSString *qrString = [[UserDefaultsUtil shareInstance]getString:LC_INQRSTRINGINFO];
    
    return [self parseStringToQRInfo:qrString];
    
}


/**
 *  保存进入的二维码信息
 *
 *  @param qrString 
 */
- (BOOL)saveInQrInfo:(NSString *)qrString{
    
    self.mInQR = [self parseStringToQRInfo:[[UserDefaultsUtil shareInstance]getString:LC_INQRSTRINGINFO]];
    
    if (self.mInQR != nil) {
        if (!([self.mInQR.mUUID caseInsensitiveCompare:@"all"] == NSOrderedSame)) {
            return NO;
        }
    }
    
    if (![self qrCodeIsLegal:qrString]) {
        return NO;
    }
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window makeToast:[NSString stringWithFormat:@"%@",[self parseStringToQRInfo:qrString]]];
    [[UserDefaultsUtil shareInstance]saveStringConfig:LC_INQRSTRINGINFO :qrString];
    
    return YES;
}

/**
 *  解析二维码信息
 *
 *  @param data 解析的二维码字符串
 *
 *  @return
 */
- (QRCodeInfo *)parseStringToQRInfo:(NSString *)data{
    if ([StringUtil isEmpty:data]) {
        return nil;
    }
    
    data = [Algorithm decString:data];
    HRLog(@"data = %@",data);
    NSArray *qrInfos = [data componentsSeparatedByString:@":"];
    if (qrInfos.count < 4) {
        return nil;
    }
    QRCodeInfo *info = [[QRCodeInfo alloc]init];
    info.mLocation = qrInfos[0];
    info.mUUID = qrInfos[1];
    info.mDateTime = [qrInfos[2] integerValue];
    info.mQRStatus = qrInfos[3];
    return info;
}

/**
 *
 *
 *  @param quitQrInfos quitQrInfos
 *
 *  @return
 */
- (BOOL)quit:(NSString *)quitQrInfos{
    QRCodeInfo *info = [self parseStringToQRInfo:quitQrInfos];
    self.mInQR = [self getInQrInfo];
    if (self.mInQR == nil) {
        return NO;
    }
    if (![self.mInQR quitQRisLegal:info]) {
        return NO;
    }
    
    [[UserDefaultsUtil shareInstance]saveStringConfig:LC_INQRSTRINGINFO :nil];
    mInstance = nil;
    _mInQR = nil;
    return YES;
}

- (BOOL)isQuitQRcode:(NSString *)qrcodeInfo{
    QRCodeInfo *info = [self parseStringToQRInfo:qrcodeInfo];
    if (info == nil || [info.mQRStatus caseInsensitiveCompare:@"out"] == NSOrderedSame) {
        return YES;
    }
    return NO;
}


- (BOOL)qrCodeIsLegal:(NSString *)info{
    NSString *userID = [[UserDefaultsUtil shareInstance]getString:LC_USERGUID];
    const char* buff = [userID UTF8String];
    long len = strlen(buff);
    unsigned long crc = hcrc32(buff, (int)len,  0xffffffff);
    NSString *mUuid = [NSString stringWithFormat:@"%ld",crc];
    QRCodeInfo *qrcodeInfo = [self parseStringToQRInfo:info];
     NSLog(@"userID = %@,mUuid = %@ info = %@",userID,mUuid,info);
    if (qrcodeInfo == nil) {
        return NO;
    }
    
    if ([mUuid isEqualToString:qrcodeInfo.mUUID]) {
        return YES;
    }
    
    if ([qrcodeInfo.mUUID isEqualToString:@"all"]) {
        return YES;
    }
    
    return NO;
}

- (NSString *)getLocation{
    if (_mInQR == nil) {
        _mInQR = [self getInQrInfo];
    }
    
    return _mInQR.mLocation;
}
@end
